namespace KYO
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Data.SqlClient;
	using System.Text;
	
	
	public class tblCatagory : Object
	{
		
		#region Static SQL String Memebers
		/// <remarks>This field represents the full SELECT string for the table tblCatagory, with the WHERE clause.</remarks>
		internal static string _SQL_Select = "SELECT [CatagoryID], [ParentID], [Name] FROM [dbo].[tblCatagory] WHERE [CatagoryID]=@CatagoryID ";
		
		/// <remarks>This field represents the full INSERT INTO string for the table tblCatagory.</remarks>
		internal static string _SQL_Insert = "INSERT INTO [dbo].[tblCatagory] ([CatagoryID], [ParentID], [Name]) VALUES([CatagoryID], [ParentID], [Name]) ";
		
		/// <remarks>This field represents the full UPDATE string for the table tblCatagory, with the WHERE clause.</remarks>
		internal static string _SQL_Update = "UPDATE [dbo].[tblCatagory] SET [CatagoryID] = @CatagoryID, [ParentID] = @ParentID, [Name] = @Name WHERE [CatagoryID]=@CatagoryID ";
		
		/// <remarks>This field represents the DELETE string for the table tblCatagory, with the WHERE clause.</remarks>
		internal static string _SQL_Delete = "DELETE FROM [dbo].[tblCatagory] WHERE [CatagoryID]=@CatagoryID ";
		#endregion
		
		#region Tables Memebers
		/// <remarks>SQL Type:int - </remarks>
		private int _CatagoryID;
		
		[DisplayName("Catagory ID")]
		[Category("Primary Key")]
		public int CatagoryID
		{
			get
			{
				try
				{
					return _CatagoryID;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting CatagoryID", err);
				}
			}
			set
			{
				try
				{
					_CatagoryID = value;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting CatagoryID", err);
				}
			}
		}
		
		/// <remarks>SQL Type:int - </remarks>
		private int _ParentID;
		
		[DisplayName("Parent ID")]
		[Category("Foreign Key")]
		public int ParentID
		{
			get
			{
				try
				{
					return _ParentID;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting ParentID", err);
				}
			}
			set
			{
				try
				{
					_ParentID = value;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting ParentID", err);
				}
			}
		}
		
		/// <remarks>SQL Type:nvarchar - </remarks>
		private string _Name;
		
		[DisplayName("Name")]
		[Category("Column")]
		public string Name
		{
			get
			{
				try
				{
					return _Name;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting Name", err);
				}
			}
			set
			{
				try
				{
					if ((value.Length <= 100))
					{
						_Name = value;
					}
					else
					{
						throw new OverflowException("Error setting Name, Length of value is to long. Maximum Length: 100");
					}
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting Name", err);
				}
			}
		}
		#endregion
		
		#region Related Objects
		/// <remarks>Represents the foreign key object</remarks>
		private tblCatagory _ParentID_tblCatagory;
		
		[Description("Represents the foreign key object of the type tblCatagory")]
		public tblCatagory ParentID_tblCatagory
		{
			get
			{
				try
				{
					return _ParentID_tblCatagory;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting ParentID_tblCatagory", err);
				}
			}
			set
			{
				try
				{
					_ParentID_tblCatagory = value;
					_ParentID = _ParentID_tblCatagory.CatagoryID;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting ParentID_tblCatagory", err);
				}
			}
		}
		#endregion
		
		#region Related Object Collections
		/// <remarks>Represents the foreign key object</remarks>
		private tblInforInCatagoryCollection _tblInforInCatagoryCollection;
		
		[Description("Represents the foreign key relation. This is an Collection of tblCatagory.")]
		public tblInforInCatagoryCollection tblInforInCatagoryCollection
		{
			get
			{
				try
				{
					return _tblInforInCatagoryCollection;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting tblInforInCatagoryCollection", err);
				}
			}
			set
			{
				try
				{
					_tblInforInCatagoryCollection = value;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting tblInforInCatagoryCollection", err);
				}
			}
		}
		#endregion
		
		#region Public Methods
		public void Select(string ConnectionString)
		{
			try
			{
				SqlConnection Conn = new SqlConnection(ConnectionString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = tblCatagory._SQL_Select;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				SqlDataReader rs = Com.ExecuteReader();
				while(rs.Read())
				{
					AddFromRecordSet(rs);
				}
				rs.Close();
				Conn.Close();
				rs.Dispose();
				Com.Dispose();
				Conn.Dispose();
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Insert(string ConnectionString)
		{
			try
			{
				SqlConnection Conn = new SqlConnection(ConnectionString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = tblCatagory._SQL_Insert;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Update(string ConnectionString)
		{
			try
			{
				SqlConnection Conn = new SqlConnection(ConnectionString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = tblCatagory._SQL_Update;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Delete(string ConnectionString)
		{
			try
			{
				SqlConnection Conn = new SqlConnection(ConnectionString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = tblCatagory._SQL_Delete;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public string[] GetSqlCommandStrings()
		{
			try
			{
				string[] CommStr = new string[4];
				CommStr[0] = "SELECT [CatagoryID], [ParentID], [Name] FROM [dbo].[tblCatagory] WHERE [CatagoryID] = " + _CatagoryID + "";
				CommStr[1] = "INSERT INTO [dbo].[tblCatagory] ([CatagoryID], [ParentID], [Name]) VALUES(" + _CatagoryID + ", " + _ParentID + ", '" + _Name.Replace("'","''") + "')";
				CommStr[2] = "UPDATE [dbo].[tblCatagory] SET [ParentID] = " + _ParentID + " ,[Name] = '" + _Name.Replace("'","''") + "' WHERE [CatagoryID] = " + _CatagoryID + "";
				CommStr[3] = "DELETE FROM [dbo].[tblCatagory] WHERE [CatagoryID] = " + _CatagoryID + "";
				return CommStr;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		#endregion
		
		#region Private Methods
		private void AddFromRecordSet(SqlDataReader rs)
		{
			try
			{
				// if value from the recordset, to the CatagoryID field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("CatagoryID")) == false))
				{
					CatagoryID = rs.GetInt32(rs.GetOrdinal("CatagoryID"));
				}
				// if value from the recordset, to the ParentID field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("ParentID")) == false))
				{
					ParentID = rs.GetInt32(rs.GetOrdinal("ParentID"));
				}
				// if value from the recordset, to the Name field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("Name")) == false))
				{
					Name = rs.GetString(rs.GetOrdinal("Name"));
				}
			}
			catch (SqlException sqlExc)
			{
				throw sqlExc;
			}
			catch (Exception Exc)
			{
				throw Exc;
			}
		}
		
		private SqlParameter[] GetSqlParameters()
		{
			List<SqlParameter> SqlParmColl = new List<SqlParameter>();
            
			try
			{
				SqlParmColl.Add(SqlUtil.AddSqlParm("@CatagoryID", CatagoryID, SqlDbType.Int));
				SqlParmColl.Add(SqlUtil.AddSqlParm("@ParentID", ParentID, SqlDbType.Int));
				SqlParmColl.Add(SqlUtil.AddSqlParm("@Name", Name, SqlDbType.NVarChar));
				return SqlParmColl.ToArray();
			}
			catch (Exception Exc)
			{
				throw Exc;
			}
		}
		#endregion
	}
	
	public class tblCatagoryCollection : System.Collections.CollectionBase
	{
		
		[Description("Adds a new tblCatagory to the collection.")]
		public int Add(tblCatagory item)
		{
			int newindex = List.Add(item);
			return newindex;
		}
		
		[Description("Removes a tblCatagory from the collection.")]
		public void Remove(tblCatagory item)
		{
			List.Remove(item);
		}
		
		[Description("Inserts an tblCatagory into the collection at the specified index.")]
		public void Insert(int index, tblCatagory item)
		{
			List.Insert(index, item);
		}
		
		[Description("Returns the index value of the tblCatagory class in the collection.")]
		public int IndexOf(tblCatagory item)
		{
			return List.IndexOf(item);
		}
		
		[Description("Returns true if the tblCatagory class is present in the collection.")]
		public bool Contains(tblCatagory item)
		{
			return List.Contains(item);
		}
	}
	
	public enum tblCatagory_Fields
	{
		
		CatagoryID,
		
		ParentID,
		
		Name,
	}
}
